package form.AssetForm;


import java.sql.Date;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JTabbedPane;

import customGUI.selectors.AssetSelector;

import other.CodeDescriptionPair;
import other.ObservableJTabbedPane;


import db.Asset;
import form.Form;
import form.FormInputComponent;

public class AssetForm extends Form implements Observer{
	
	private AssetSelector selector;
	private FormInputComponent assetName;
	private FormInputComponent owner;
	private FormInputComponent custodian;
	private FormInputComponent type;
	private FormInputComponent dateAcquired;
	private FormInputComponent retentionPeriod;
	private FormInputComponent maintenanceSched;
	private FormInputComponent financialVal;
	private FormInputComponent confidentialityVal;
	private FormInputComponent integrityVal;
	private FormInputComponent availabilityVal;
	private FormInputComponent classification;
	private FormInputComponent storageLocation;
	
	public AssetForm(){
		inputComponents = new ArrayList<FormInputComponent>();
	}
		
	public AssetSelector getSelector() {
		return selector;
	}

	public void setSelector(AssetSelector selector) {
		this.selector = selector;
	}

	public FormInputComponent getAssetName() {
		return assetName;
	}
	public void setAssetName(FormInputComponent name) {
		this.assetName = name;
	}
	public FormInputComponent getOwner() {
		return owner;
	}
	public void setOwner(FormInputComponent owner) {
		this.owner = owner;
	}
	public FormInputComponent getCustodian() {
		return custodian;
	}
	public void setCustodian(FormInputComponent custodian) {
		this.custodian = custodian;
	}
	public FormInputComponent getType() {
		return type;
	}
	public void setType(FormInputComponent type) {
		this.type = type;
	}
	public FormInputComponent getDateAcquired() {
		return dateAcquired;
	}
	public void setDateAcquired(FormInputComponent dateAcquired) {
		this.dateAcquired = dateAcquired;
	}
	public FormInputComponent getRetentionPeriod() {
		return retentionPeriod;
	}
	public void setRetentionPeriod(FormInputComponent retentionPeriod) {
		this.retentionPeriod = retentionPeriod;
	}
	public FormInputComponent getMaintenanceSched() {
		return maintenanceSched;
	}
	public void setMaintenanceSched(FormInputComponent maintenanceSched) {
		this.maintenanceSched = maintenanceSched;
	}
	public FormInputComponent getFinancialVal() {
		return financialVal;
	}
	public void setFinancialVal(FormInputComponent financialVal) {
		this.financialVal = financialVal;
	}
	public FormInputComponent getConfidentialityVal() {
		return confidentialityVal;
	}
	public void setConfidentialityVal(FormInputComponent confidentialityVal) {
		this.confidentialityVal = confidentialityVal;
	}
	public FormInputComponent getIntegrityVal() {
		return integrityVal;
	}
	public void setIntegrityVal(FormInputComponent integrityVal) {
		this.integrityVal = integrityVal;
	}
	public FormInputComponent getAvailabilityVal() {
		return availabilityVal;
	}
	public void setAvailabilityVal(FormInputComponent availabilityVal) {
		this.availabilityVal = availabilityVal;
	}
	public FormInputComponent getClassification() {
		return classification;
	}
	public void setClassification(FormInputComponent classification) {
		this.classification = classification;
	}
	public FormInputComponent getStorageLocation() {
		return storageLocation;
	}
	public void setStorageLocation(FormInputComponent storageLocation) {
		this.storageLocation = storageLocation;
	}
	
	@Override
	public void clear() {
		for(int i=0;i<inputComponents.size();i++)
			inputComponents.get(i).returnUIToDefaultState();
	}
	
	@Override
	public void refresh() {
		selector.refreshAssetChoices();
	}
	
	public void addComponentsToList(){
		inputComponents.add(assetName);
		inputComponents.add(owner);
		inputComponents.add(custodian);
		inputComponents.add(type);
		inputComponents.add(dateAcquired);
		inputComponents.add(retentionPeriod);
		inputComponents.add(maintenanceSched);
		inputComponents.add(financialVal);
		inputComponents.add(confidentialityVal);
		inputComponents.add(integrityVal);
		inputComponents.add(availabilityVal);
		inputComponents.add(classification);
		inputComponents.add(storageLocation);
	}
		
	public void triggerAllInputUIErrorIfAny(){
		String currError;
		for(int i=0;i<inputComponents.size();i++)
		{
			if(!inputComponents.get(i).hasValidInput()){
				currError = inputComponents.get(i).getErrorMsg();
				if(currError!=null && !currError.equals(""))
					inputComponents.get(i).triggerUIError();
			}
		}
	}
	
	public void populate(int assetID){
		Asset asset = Asset.createAssetFromID(assetID);
		populate(asset);
	}
	
	public void populate(Asset asset){
		if(asset!= null){
			assetName.setInput(asset.getName());
			owner.setInput(asset.getOwner());
			custodian.setInput(asset.getCustodian());
			storageLocation.setInput(asset.getStorageLocation());

			dateAcquired.setInput(asset.getDateAcquired().toString());
			retentionPeriod.setInput(asset.getRetentionPeriod().toString());

			classification.setInput(asset.getClassificationDesc());
			type.setInput(asset.getTypeDesc());
			maintenanceSched.setInput(asset.getMaintenanceScheduleDesc());
			financialVal.setInput(asset.getFinancialDesc());
			confidentialityVal.setInput(asset.getConfidentialityDesc());
			integrityVal.setInput(asset.getIntegrityDesc());
			availabilityVal.setInput(asset.getAvailabilityDesc());
		}
	}
	
	public void updateAssetInfo(Asset asset){
		if(asset!=null){
			asset.setName(assetName.getInput());
			asset.setOwner(owner.getInput());
			asset.setCustodian(custodian.getInput());
			asset.setStorageLocation(storageLocation.getInput());
			
			asset.setDateAcquired(Date.valueOf(dateAcquired.getInput()));
			asset.setRetentionPeriod(Date.valueOf(retentionPeriod.getInput()));
			
			//Since only the asset's code can be replaced, you have to use the static getCode() method in CodeDescriptionPair to get the corresponding code given the description
			asset.setClassificationCode(CodeDescriptionPair.getCode(CodeDescriptionPair.TABLE_CLASSIFICATION, classification.getInput()));
			asset.setTypeCode(CodeDescriptionPair.getCode(CodeDescriptionPair.TABLE_TYPE,  type.getInput()));
			asset.setMaintenanceScheduleCode(CodeDescriptionPair.getCode(CodeDescriptionPair.TABLE_MINTENANCE, maintenanceSched.getInput()));
			asset.setFinancialCode(CodeDescriptionPair.getCode(CodeDescriptionPair.TABLE_FINANCIAL, financialVal.getInput()));
			asset.setConfidentialityCode(CodeDescriptionPair.getCode(CodeDescriptionPair.TABLE_CONFIDENTIALITY, confidentialityVal.getInput()));
			asset.setIntegrityCode(CodeDescriptionPair.getCode(CodeDescriptionPair.TABLE_INTEGRITY, integrityVal.getInput()));
			asset.setAvailabilityCode(CodeDescriptionPair.getCode(CodeDescriptionPair.TABLE_AVAILABILITY, availabilityVal.getInput()));
		}
		
	}

	@Override
	public void update(Observable arg0, Object arg1) {
			JTabbedPane tabbedPane = ((ObservableJTabbedPane) arg0).getJTabbedPane();
			
			if(tabbedPane.getSelectedComponent() == this && selector != null)
			{
				selector.refreshAssetChoices();
			}
		
	}
	
}
